package com.sz.jinzheng.service.impl;

import com.sz.jinzheng.mapper.StaffExminationMapper;
import com.sz.jinzheng.mapper.TaskMapper;
import com.sz.jinzheng.model.StaffExmination;
import com.sz.jinzheng.model.Task;
import com.sz.jinzheng.service.StaffExaminationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class StaffExaminationServiceImpl implements StaffExaminationService {
    @Autowired
    private StaffExminationMapper staffExminationMapper;
    @Autowired
    private TaskMapper taskMapper;

    @Transactional
    @Override
    public int updateStaffExmination(StaffExmination staffExmination) {
        //更新自己的任务表 也就是审批了
        int i = staffExminationMapper.updateStaffExmination(staffExmination);
        StaffExmination exminationById = staffExminationMapper.getStaffExminationById(staffExmination.getId());
        Task task2 = new Task();
        StaffExmination staffExmination1 = new StaffExmination();
        //如果更新成功那么就把任务表的状态也更新 去进行下一步
        if (i > 0) {
            //首先查询出领取的对应任务的状态是什么
            task2.setId(exminationById.getTaskId());
            Task task = taskMapper.selectByKey(task2);
            if (task.getStatus() != 8) {
                //如果任务状态不等于8说明任务还没结束
                if (task.getStatus() == 3) {
                    //如果任务状态为3 那么就代表现在是在进行第二次审批
                    // 就需要判断两次的分数差是否有差距 若两个审查员的评分差异在2分以上或审批结果不同就需要交给第三个审查员审批
                    //也就是状态改为 4 反之则状态改为6(待业务经理领取)
                    staffExmination1.setTaskId(staffExmination.getTaskId());
                    //获得第一次的任务信息
                    staffExmination1.setExaminationNum(1);
                    StaffExmination staff = staffExminationMapper.getStaffExminationByKey(staffExmination1);
                    //材料真实性评分差
                    int a = staff.getMaterialAuthenticityScore() - staffExmination.getMaterialAuthenticityScore();
                    //个人评分差
                    int b = staff.getApplicantEvaluation() - staffExmination.getApplicantEvaluation();
                    //分差在2分以上  评审结果不同
                    if (Math.abs(a) < 2 || Math.abs(b) < 2 || staff.getStatus() != staffExmination.getStatus()) {
                        //进入第三次审查
                        task2.setStatus(task.getStatus() + 1);
                    } else {
                        //结束审查专员审查 进入业务经理审查
                        task2.setStatus(6);
                    }
                } else {
                    //正常执行
                    task2.setStatus(task.getStatus() + 1);
                }
                //更新状态
                int i1 = taskMapper.updateTaskStatus(task2);
                log.info(String.valueOf(i1));
            }
        }
        return i;
    }
}
